/*
 * Germinus
 */
package org.germinus.telcoblocks.infraestructuras.diagram.rcp.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.germinus.telcoblocks.TelcoblocksPackage;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Centralita_VOIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Centralita_VOIPIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.EnlaceComentarioEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.EnlaceEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.MediaServerEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.MediaServerIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.REDEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Servidor_APP_SIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Servidor_APP_SIPIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Servidor_VOIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Servidor_VOIPIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.SoftPhone_VOIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.SoftPhone_VOIPIDEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Telefono_VOIPEditPart;
import org.germinus.telcoblocks.infraestructuras.diagram.rcp.edit.parts.Telefono_VOIPIDEditPart;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class TelcoblocksVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "telcoblocks.infraestructuras.diagram.rcp/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (REDEditPart.MODEL_ID.equals(view.getType())) {
				return REDEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return org.germinus.telcoblocks.infraestructuras.diagram.rcp.part.TelcoblocksVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				TelcoblocksInfraestructurasDiagramEditorPlugin.getInstance()
						.logError(
								"Unable to parse view type as a visualID number: "
										+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return String.valueOf(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (TelcoblocksPackage.eINSTANCE.getRED().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((org.germinus.telcoblocks.RED) domainElement)) {
			return REDEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = org.germinus.telcoblocks.infraestructuras.diagram.rcp.part.TelcoblocksVisualIDRegistry
				.getModelID(containerView);
		if (!REDEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (REDEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.germinus.telcoblocks.infraestructuras.diagram.rcp.part.TelcoblocksVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = REDEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case REDEditPart.VISUAL_ID:
			if (TelcoblocksPackage.eINSTANCE.getSoftPhone_VOIP().isSuperTypeOf(
					domainElement.eClass())) {
				return SoftPhone_VOIPEditPart.VISUAL_ID;
			}
			if (TelcoblocksPackage.eINSTANCE.getServidor_VOIP().isSuperTypeOf(
					domainElement.eClass())) {
				return Servidor_VOIPEditPart.VISUAL_ID;
			}
			if (TelcoblocksPackage.eINSTANCE.getMediaServer().isSuperTypeOf(
					domainElement.eClass())) {
				return MediaServerEditPart.VISUAL_ID;
			}
			if (TelcoblocksPackage.eINSTANCE.getServidor_APP_SIP()
					.isSuperTypeOf(domainElement.eClass())) {
				return Servidor_APP_SIPEditPart.VISUAL_ID;
			}
			if (TelcoblocksPackage.eINSTANCE.getCentralita_VOIP()
					.isSuperTypeOf(domainElement.eClass())) {
				return Centralita_VOIPEditPart.VISUAL_ID;
			}
			if (TelcoblocksPackage.eINSTANCE.getTelefono_VOIP().isSuperTypeOf(
					domainElement.eClass())) {
				return Telefono_VOIPEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = org.germinus.telcoblocks.infraestructuras.diagram.rcp.part.TelcoblocksVisualIDRegistry
				.getModelID(containerView);
		if (!REDEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (REDEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.germinus.telcoblocks.infraestructuras.diagram.rcp.part.TelcoblocksVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = REDEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case SoftPhone_VOIPEditPart.VISUAL_ID:
			if (SoftPhone_VOIPIDEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Servidor_VOIPEditPart.VISUAL_ID:
			if (Servidor_VOIPIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case MediaServerEditPart.VISUAL_ID:
			if (MediaServerIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Servidor_APP_SIPEditPart.VISUAL_ID:
			if (Servidor_APP_SIPIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Centralita_VOIPEditPart.VISUAL_ID:
			if (Centralita_VOIPIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Telefono_VOIPEditPart.VISUAL_ID:
			if (Telefono_VOIPIDEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case REDEditPart.VISUAL_ID:
			if (SoftPhone_VOIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Servidor_VOIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (MediaServerEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Servidor_APP_SIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Centralita_VOIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Telefono_VOIPEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case EnlaceEditPart.VISUAL_ID:
			if (EnlaceComentarioEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (TelcoblocksPackage.eINSTANCE.getEnlace().isSuperTypeOf(
				domainElement.eClass())) {
			return EnlaceEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(org.germinus.telcoblocks.RED element) {
		return true;
	}

}
